<p>
    <?= $this->App->buttonLink('+ Nova Kampanja', '/campaigns/create') ?>
    <?= $this->Html->link('Fakture', '/invoices') ?> |
    <?= $this->Html->link('Uplate', '/payments') ?>
</p>

<?= $this->element('entity_table', array(
    'columns' => array(
        'Id'            => 'Campaign.id',
        'Naziv'         => 'Campaign.name',
        'Lokacije'      => array(false, function($row){
            $items = array();
            foreach($row['Location'] as $location) {
                $items[] = '<p>' . 
                    $location['Location']['name'] . ' (' .
                    date_local($location['CampaignsLocation']['start']) . ' - ' .
                    date_local($location['CampaignsLocation']['end']) .
                    ')</p>';
            }
            
            return implode('', $items);
        }),
        'Klijent'       => 'Client.name',
        'Rabat'         => array('Campaign.rabat', function($row) {
        	$rabats = Set::extract('{n}.CampaignsLocation.rabat', $row['Location']);
			$ret = array();
            foreach ($rabats as $rabat) {
				$ret[] = sprintf("%0.2f", (float)$rabat) . "%";
			}
			return implode("<br>", $ret);
        }),
        'Avans'         => array('Campaign.avans', function($row) {
        	$avanses = Set::extract('{n}.CampaignsLocation.avans', $row['Location']);
			$ret = array();
			foreach ($avanses as $avans) {
				$ret[] = sprintf("%0.2f", (float)$avans) . "%";
			}
			return implode("<br>", $ret);
        })
    ),
    'columnWidth' => array(
        'Id' => '1%'
    ),
    'links' => array(
        'Naziv'       => array('/campaigns/edit/%d', 'Campaign.id'),
        'Lokacija' => array('/locations/edit/%d', 'Location.id'),
        'Klijent'  => array('/clients/edit/%d', 'Client.id')
    ),
    'align' => array(
    	'Rabat' => 'right',
    	'Avans' => 'right'
	),
    'filter' => array(
        'Naziv'    => ''.@$filter['Campaign']['name'],
        'Lokacija' => ''.@$filter['Location']['name'],
        'Klijent'  => ''.@$filter['Client']['name']
    ),
    'data'     => $campaigns,
    'id'       => 'Campaign.id',
    'entity'   => 'campaigns',
    'sort_by'  => $sort_by,
    'sort_dir' => $sort_dir,
    'actions'  => array(
        array(
            'label' => 'Kreiraj fakturu',
            'link'  => '/invoices/create/?campaign=%s'
        )
    )
)) ?>
